package com.htd.erp.web;

import java.util.Map;
import java.util.Map.Entry;

import org.richfaces.component.SortOrder;

import com.google.common.collect.Maps;

/**
 * @author Zhang Guilin
 *
 */
public class DataTable {
	
	private int pageSize = 10;
	private Map<String, SortOrder> sortOrders = Maps.newHashMapWithExpectedSize(1);
    private Map<String, String> filterValues = Maps.newHashMap();
    private String sortProperty;
    public void toggleSort() {
        for (Entry<String, SortOrder> entry : sortOrders.entrySet()) {
            SortOrder newOrder;
            if (entry.getKey().equals(sortProperty)) {
                if (entry.getValue() == SortOrder.ascending) {
                    newOrder = SortOrder.descending;
                } else {
                    newOrder = SortOrder.ascending;
                }
            } else {
                newOrder = SortOrder.unsorted;
            }
            entry.setValue(newOrder);
        }
    }
    
    public DataTable registProperty(String property) {
    	sortOrders.put(property, SortOrder.unsorted);
    	return this;
    }
    
    public SortOrder getAsc() {
    	return SortOrder.ascending;
    }
    
    public SortOrder getDesc() {
    	return SortOrder.descending;
    }
    
    public SortOrder getUnsorted() {
    	return SortOrder.unsorted;
    }
    
    public SortOrder getSortOrder(String property) {
    	SortOrder sortOrder = sortOrders.get(property);
    	return sortOrder == null ? SortOrder.unsorted : sortOrder;
    }
    
    public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public Map<String, String> getFilterValues() {
        return filterValues;
    }
    
    public Map<String, SortOrder> getSortOrders() {
        return sortOrders;
    }
 
    public String getSortProperty() {
        return sortProperty;
    }
 
    public void setSortProperty(String sortPropety) {
        this.sortProperty = sortPropety;
    }
}
